﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCPnuts.ISkyDiskFileSystem.Interfaces;
using CCPnuts.ISkyDiskFileSystem.Exceptions;
using CCPnuts.Pack;
using System.IO;

namespace CCPnuts.ISkyDiskFileSystem
{
    public class SkyDirectory : SkyElementContainer
    {
        #region Packed Target

        internal SkyDirectory(IPacked tar) : base(tar) { }

        internal new I_SkyDirectory Packed { get { return (I_SkyDirectory)base.Packed; } }

        #endregion

        #region Own Members

        private bool HasElement(SkyElementTypes type, string name)
        {
            var tar = from ele in Children
                      where ele.ElementType == type && ele.Name == name
                      select ele;

            int count = tar.Count();
            if (count > 1)
            {
                throw new ImplementError(String.Format("There can not be 2 {0} with the same name.", type));
            }
            return (count == 1) ? true : false;
        }

        private SkyElement GetElement(SkyElementTypes type, string name)
        {
            var tar = from ele in Children
                      where ele.ElementType == type && ele.Name == name
                      select ele;
            int count = tar.Count();

            if (count > 1)
            {
                throw new ImplementError(String.Format("There can not be 2 {0} with the same name.", type));
            }
            else if (count == 1)
            {
                return (SkyElement)tar.First();
            }
            else
            {
                throw new SkyElementException(String.Format("The {0} : {1} can not be found.", type, name));
            }
        }

        public override SkyElementTypes ElementType
        {
            get { return SkyElementTypes.Directory; }
        }

        #endregion
    }

    public static partial class PackedInfos
    {
        public static PackedInfo SkyDirectory { get { return new PackedInfo(p => new SkyDirectory(p)); } }
    }
}
